package com.chalk.util;


import cn.hutool.core.codec.Base64;
import com.chalk.util.wxminipay.WxMiniPayConfig;
import com.chalk.util.wxminipay.WxMiniRequestParam;
import org.apache.commons.codec.digest.DigestUtils;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class WxUtil {
    public static void main(String[] args) throws Exception {
        String json = "{\"PushTime\":\"2020-12-25 21:37:33\",\"EBusinessID\":\"test1682707\",\"Data\":[{\"LogisticCode\":\"1234561\",\"ShipperCode\":\"SF\",\"Traces\":[{\"AcceptStation\":\"顺丰速运已收取快件\",\"AcceptTime\":\"2020-12-25 21:37:33\",\"Remark\":\"\"},{\"AcceptStation\":\"货物已经到达深圳\",\"AcceptTime\":\"2020-12-25 21:37:332\",\"Remark\":\"\"},{\"AcceptStation\":\"货物到达福田保税区网点\",\"AcceptTime\":\"2020-12-25 21:37:333\",\"Remark\":\"\"},{\"AcceptStation\":\"货物已经被张三签收了\",\"AcceptTime\":\"2020-12-25 21:37:334\",\"Remark\":\"\"}],\"State\":\"3\",\"EBusinessID\":\"test1682707\",\"Success\":true,\"Reason\":\"\",\"CallBack\":\"\",\"EstimatedDeliveryTime\":\"2020-12-25 21:37:33\"}],\"Count\":\"1\"}24615d86-7553-4b69-ae54-160dcc8d700f";
        String md52 = Md5Util.encode(json);
        System.out.println(md52.length());
        ;
        String md5=DigestUtils.md5Hex(json).toLowerCase();
        System.out.println(md5);
        String b64 = Base64.encode(md5);
        Base64.encodeUrlSafe(md5.toLowerCase());
        System.out.println(Base64.encodeUrlSafe(md5));
        System.out.println(b64);
        String finStr = URLEncoder.encode(b64, "utf-8");
        System.out.println(finStr);
    }

    public static String generatorPaySignForMD5(WxMiniPayConfig wxPayParam) {
        String param = "appid=" + WxMiniPayConfig.appid + "&body=" + wxPayParam.getBody() + "&mch_id=" + WxMiniPayConfig.mch_id + "&nonce_str=" + wxPayParam.getNonce_str() +
                "&notify_url=" + WxMiniPayConfig.notify_url + "&openid=" + wxPayParam.getOpen_id() + "&out_trade_no=" + wxPayParam.getOut_trade_no() + "&spbill_create_ip=" + wxPayParam.getSpbill_create_ip() +
                "&total_fee=" + wxPayParam.getTotal_fee() + "&trade_type=" + WxMiniPayConfig.trade_type;

        System.out.println(param);
        String signTemp = param + "&key=" + WxMiniPayConfig.key;
        String sign = DigestUtils.md5Hex(signTemp).toUpperCase();

        return sign;
    }

    public static String generatorQueryForMD5(WxMiniPayConfig config) {
        String param = "appid=" + WxMiniPayConfig.appid + "&mch_id=" + WxMiniPayConfig.mch_id + "&nonce_str=" + config.getNonce_str() + "&out_trade_no=" + config.getOut_trade_no();
        System.out.println(param);
        String signTemp = param + "&key=" + WxMiniPayConfig.key;
        String sign = DigestUtils.md5Hex(signTemp).toUpperCase();
        return sign;
    }

    public static String generatorPaySign(WxMiniRequestParam miniRequestParam) {
        String param = "appId=" + miniRequestParam.getAppid() + "&nonceStr=" + miniRequestParam.getNonceStr() + "&package=" + miniRequestParam.getPackageQ() +
                "&signType=" + miniRequestParam.getSignType() + "&timeStamp=" + miniRequestParam.getTimeStamp() + "&key=" + miniRequestParam.getKey();
        String paySign = DigestUtils.md5Hex(param).toUpperCase();
        System.out.println(param);
        return paySign;
    }

}

class Md5Util {
    /**
     * md5加密的方法
     *
     * @param text
     * @return
     */
    public static String encode(String text) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] result = digest.digest(text.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : result) {
                int number = b & 0xff;
                String hex = Integer.toHexString(number);
                if (hex.length() == 1) {
                    sb.append("0");
                }
                sb.append(hex);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }
}